package adc.app.ui.wicket.valid;

import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IDetachable;

class DetachValidatorsBehavior extends Behavior {

	private static final long serialVersionUID = 1L;

	@Override
	public void detach(Component component) {
		if (!(component instanceof FormComponent<?>)) {
			return;
		}
		FormComponent<?> formComponent = (FormComponent<?>) component;
		List<?> validators = formComponent.getValidators();
		for (Object validator : validators) {
			if (validator instanceof IDetachable) {
				IDetachable detachable = (IDetachable) validator;
				detachable.detach();
			}
		}
		super.detach(component);
	}

}
